<?php
/**
 * +-------------------------------------------------------------
 * |Caption:
 * +-------------------------------------------------------------
 * |Author: Lenny
 * +-------------------------------------------------------------
 * |Email:  1685964109@qq.com
 * +-------------------------------------------------------------
 * |Date:   2024-03-04 08:51
 * +-------------------------------------------------------------
 */

namespace storage\engine;

use OSS\Core\OssException;
use OSS\Http\RequestCore_Exception;
use OSS\OssClient;

class Alioss extends Base {
    /**
     * 执行上传
     * @return bool
     * @throws RequestCore_Exception
     */
    public function upload(): bool
    {
        try {
            $ossClient = new OssClient(
                    $this->config['accessKeyId'],
                    $this->config['accessSecret'],
                    $this->config['domain'],
                    true
            );
            $ossClient->uploadFile(
                    $this->config['bucket'],
                    $this->getSaveFileInfo()['file_path'],
                    $this->getRealPath()
            );
        } catch (OssException $e) {
            $this->error = $e->getMessage();
            return false;
        }
        return true;
    }

    /**
     * 删除文件
     * @param string $filePath
     * @return bool
     * @throws RequestCore_Exception
     */
    public function delete(string $filePath): bool
    {
        try {
            $ossClient = new OssClient(
                    $this->config['accessKeyId'],
                    $this->config['accessSecret'],
                    $this->config['domain'],
                    true
            );
            $ossClient->deleteObject($this->config['bucket'], $filePath);
        } catch (OssException $e) {
            $this->error = $e->getMessage();
            return false;
        }
        return true;
    }
}